package io.netty.example.test.buffer;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;

import java.nio.charset.Charset;

public class NettyByteBuf {
    public static void main(String[] args) {
        ByteBuf buffer = Unpooled.buffer(10);
        System.out.println("buffer:"+buffer);
        for (int i = 0; i < 8; i++) {
            buffer.writeInt(i);
        }
        System.out.println("buffer:"+buffer);

        for (int i = 0; i < 5; i++) {
            System.out.println(buffer.getByte(i));
        }
        System.out.println("buffer:"+buffer);

        for (int i = 0; i < 5; i++) {
            System.out.println(buffer.readByte());
        }
        System.out.println("buffer:"+buffer);


        ByteBuf copiedBuffer = Unpooled.copiedBuffer("hello world", CharsetUtil.UTF_8);
        if (copiedBuffer.hasArray()) {
            byte[] content = copiedBuffer.array();
            System.out.println(new String(content, CharsetUtil.UTF_8));
            System.out.println(copiedBuffer);
            System.out.println(copiedBuffer.readerIndex());
            System.out.println(copiedBuffer.writerIndex());
            System.out.println(copiedBuffer.capacity());
            System.out.println("=============");
            System.out.println(copiedBuffer.getByte(1));
            System.out.println(copiedBuffer.readableBytes());
            for (int i = 0; i < copiedBuffer.readableBytes(); i++) {
                System.out.println(copiedBuffer.getByte(i));
            }

            System.out.println(copiedBuffer.getCharSequence(0, 6, CharsetUtil.UTF_8));
            System.out.println(copiedBuffer.getCharSequence(6,6,CharsetUtil.UTF_8));
        }
    }
}
